package com.huyn.baseframework.net;

import android.content.Context;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.cache.ACache;
import com.android.volley.toolbox.Volley;
import com.huyn.baseframework.utils.StringUtils;

/**
 * Created by huyaonan on 16/10/8.
 */
public class HttpRequestLoader {
    
    private static HttpRequestLoader mInstance = null;
    private RequestQueue mRequestQueue;
    private ACache mCache;
    private Context mContext;

    private HttpRequestLoader(Context context) {
        mContext = context;
        mRequestQueue = Volley.newRequestQueue(context, 10);

        mCache = ACache.get(context);
    }

    public static HttpRequestLoader getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new HttpRequestLoader(context.getApplicationContext());
        }
        return mInstance;
    }

    public void stop() {
        try {
            if (mRequestQueue != null)
                mRequestQueue.stop();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void start() {
        try {
            if (mRequestQueue != null)
                mRequestQueue.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void cancelRequest(Object tag) {
        if (mRequestQueue != null)
            mRequestQueue.cancelAll(tag);
    }

    public void cancelAllRequest() {
        if (mRequestQueue != null)
            mRequestQueue.cancelAll(new RequestQueue.RequestFilter() {

                @Override
                public boolean apply(Request<?> request) {
                    return true;
                }
            });
    }


    private void startHttpLoader(Request<?> request, boolean blockInBackground) {
//		if(blockInBackground && !AppUtil.isAppOnForeground(mContext))
//			return;
        mRequestQueue.add(request);
    }

    public void startHttpLoader(Request<?> request) {
        startHttpLoader(request, true);
    }

    public Object startCacheLoader(String key, Request<?> request, boolean alwaysLoad) {
        return startCacheLoader(key, request, alwaysLoad, true);
    }

    public Object startCacheLoader(String key, Request<?> request, boolean alwaysLoad, boolean blockInBackground) {
        if (alwaysLoad) {
            startHttpLoader(request, blockInBackground);
            return null;
        }
        request.loadCache(mContext, key, !alwaysLoad);
        if (mCache == null || StringUtils.isBlank(key)) {
            startHttpLoader(request, blockInBackground);
            return null;
        } else {
            ACache.CacheFeed feed = mCache.getAsObject(key);
            if (feed == null) {
                startHttpLoader(request, blockInBackground);
                return null;
            }
            if (feed.outOfDate == true) {
                startHttpLoader(request, blockInBackground);
            }
            return feed.object;
        }
    }

    public void release() {
        this.mRequestQueue = null;
        mInstance = null;
    }
}